<?php

namespace app\admin\controller\bdl\bd;
use app\admin\model\Admin;
use app\admin\model\BdDatacenter;
use app\admin\model\BdOrg;
use app\common\controller\Backend;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Datacenter extends Backend
{
    /**
     * 模型对象
     * @var \app\admin\model\BdDatacenter
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();

        $this->model = new \app\admin\model\BdDatacenter();
        $this->view->assign("fenableList", $this->model->getFenableList());
    }


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);

        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['modifier','creator'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);



            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 所有数据筛选条件
     */
    public function getFilter()
    {
        $type = $this->request->param('type');

        switch ($type) {
            //数据中心
            case 'get_datacenter_list':
                $result = BdDatacenter::where('fenable', 1)->column('fname as name', 'fnumber');

                break;
            //组织
            case 'get_org_list':
                $result = BdOrg::where('fenable', 1)->column('fname as name', 'id');
                break;
            //业务员
            case 'get_biz_list':
                $result = \app\admin\model\Bizpersoncate::where('fenable', 1)->column('fname as name', 'id');
                break;
            //配送车
            case 'get_carcate_list':
                $result = \app\admin\model\Carinfocate::where('fenable', 1)->column('fname as name', 'id');
                break;
            //表面分类
            case 'get_surface_list':
                $result = \app\admin\model\Surfacecate::where('fenable', 1)->column('fname as name', 'id');
                break;
            //材质分类
            case 'get_texture_list':
                $result = \app\admin\model\Texturecate::where('fenable', 1)->column('fname as name', 'id');
                break;
            //分类
            case 'get_cate_list':
                $result = \app\admin\model\Materialcate::where('fenable', 1)->column('fname as name', 'id');

                break;
            //一级分类
            case 'get_cate1_list':
            $result = \app\admin\model\Materialcate::where('fenable', 1)
                                                    ->where('fk_bdl_super',0)
                                                    ->where('forgid',$this->request->param('forgid'))
                                                    ->where('fk_bdl_bd_datacenter',$this->request->param('fk_bdl_bd_datacenter'))
                                                    ->field('fname as name,id as value')->select();
                $this->success('','',$result);

                break;
            //二三级分类
            case 'get_cate2_list':
                $result = \app\admin\model\Materialcate::where('fenable', 1)
                    ->where('fk_bdl_super',$this->request->param('fk_bdl_super'))
                    ->where('forgid',$this->request->param('forgid'))
                    ->where('fk_bdl_bd_datacenter',$this->request->param('fk_bdl_bd_datacenter'))
                    ->field('fname as name,id as value')->select();
                $this->success('','',$result);
                break;

            //产品
            case 'get_material_list':
                $result = \app\admin\model\Material::where('fenable', 1)->column('fname as name', 'id');

                break;

            //材质
            case 'get_texture_list':
                $result = \app\admin\model\Texture::where('fenable', 1)->column('fname as name', 'id');

                break;

            //表面
            case 'get_surface_list':
                $result = \app\admin\model\Surface::where('fenable', 1)->column('fname as name', 'id');

                break;
            //库存
            case 'get_inventory_list':
                $result = \app\admin\model\Inventory::where('fenable', 1)->column('fk_bdl_bd_stock as name', 'id');

                break;
            //管理员
            case 'get_admin_list':
                $result = Admin::column('nickname as name', 'id');

                break;
            case 'get_datacenter_list_for_form_select':
                $result = BdDatacenter::where('fenable', 1)->field('fname as name,fnumber as value')->select();
                $this->success('','',$result);

                break;
            case 'get_org_list_for_form_select':
                $fk_bdl_bd_datacenter = $this->request->param('fk_bdl_bd_datacenter');
                $result = BdOrg::where('fenable', 1)->field('fname as name,id as value,fk_bdl_bd_datacenter')->where('fk_bdl_bd_datacenter',$fk_bdl_bd_datacenter)->select();
                $this->success('','',$result);
                break;
            case 'get_fk_bdl_bd_material_list_for_form_select':
                $forgid = $this->request->param('forgid');
                $fk_bdl_bd_datacenter = $this->request->param('fk_bdl_bd_datacenter');
                $result = \app\admin\model\Material::where('fenable', 1)
                    ->field('fname as name,id as value,fk_bdl_bd_datacenter,forgid')
                    ->where('fk_bdl_bd_datacenter',$fk_bdl_bd_datacenter)
                    ->where('forgid',$forgid)
                    ->select();
                $this->success('','',$result);
                break;
            case 'get_fk_bdl_bd_surface_list_for_form_select':
                $forgid = $this->request->param('forgid');
                $forg   = BdOrg::where('id',$forgid)->find();
                //映射
                $fnumbers                   = explode(",",$forg['fk_bdl_mappingorg']);
                $fk_bdl_mappingorgs_forgIds = BdOrg::whereIn('fnumber',$fnumbers)->column('id');

                $result = \app\admin\model\Surface::where('fenable', 1)
                    ->field('fname as name,id as value,fk_bdl_bd_datacenter,forgid')
                    ->where('fk_bdl_bd_datacenter',$forg['fk_bdl_bd_datacenter'])
                    ->whereIn('forgid',$fk_bdl_mappingorgs_forgIds)
                    ->select();
                $this->success('','',$result);
                break;
            case 'get_customer_list_for_form_select':
                $result = \app\admin\model\Customer::where('fenable', 1)
//                    ->where('fk_bdl_bd_datacenter',input('fk_bdl_bd_datacenter'))
                    ->where('forgid',input('forgid'))
                    ->field('fname as name,id as value,fk_bdl_bd_datacenter,forgid')->select();
                $this->success('','',$result);
                break;
            case 'get_fk_bdl_bdgroup_list_for_form_select':
                $forgid = $this->request->param('forgid');
                $result = \app\admin\model\Surfacecate::where('fenable', 1)->field('fname as name,id as value,fk_bdl_bd_datacenter')->where('forgid',$forgid)->select();
                $this->success('','',$result);
                break;
            case "get_texture_fk_bdl_bdgroup_list_for_form_select":
                $forgid = $this->request->param('forgid');
                $result = \app\admin\model\Texturecate::where('fenable', 1)->field('fname as name,id as value,fk_bdl_bd_datacenter')->where('forgid',$forgid)->select();
                $this->success('','',$result);
                break;
            case 'get_bizperson_list_for_form_select':
                $forgid = $this->request->param('forgid');
                $result = \app\admin\model\Bizpersoncate::where('fenable', 1)->field('fname as name,id as value,fk_bdl_bd_datacenter')->where('forgid',$forgid)->select();
                $this->success('','',$result);
                break;
            case 'get_carinfocate_list_for_form_select':
                $forgid = $this->request->param('forgid');
                $result = \app\admin\model\Carinfocate::where('fenable', 1)->field('fname as name,id as value,fk_bdl_bd_datacenter')->where('forgid',$forgid)->select();
                $this->success('','',$result);
                break;
        }
        return json($result);


    }


}